<?php

namespace app\api\controller;
use app\admin\model\GameType;
use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Category;
use app\common\model\GameCategory;
use app\common\model\GameDirectory;
use app\common\model\GameExp;
use app\common\model\GameRat;
use app\manystore\model\ManystoreShop;
use think\Db;

/**
 * 游戏目录
 */
class Game extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new GameDirectory();
    }

    //游戏列表
    public function game_list()
    {
        $where = []; // 初始化查询条件数组
        if(input('title')!=null&&input('title')!=''){
            $where['title']=['like','%'.input('title').'%'];
        }
        if(input('player_count')!=null&&input('player_count')!=''){
            $where['player_count']=input('player_count');
        }
        if(input('type_id')!=null&&input('type_id')!=''){
            $where['type_id']=input('type_id');
        }
        if(input('categoty_id')!=null&&input('categoty_id')!=''){
            $where['categoty_id']=input('categoty_id');
        }
        $query = GameDirectory::where('status' ,'=',1)->where('pla_status','=',1)->where('sh_status','<>',0);
        if (!empty($where)) {
            $query = $query->where($where);
        }
        $where['shop_id']=input('shop_id');
        $list = $query
            ->field('id, title, image_url, player_count,(select name from  fa_game_category where fa_game_category.id=type_id)typename')
            ->order('weigh desc')
            ->paginate(10);
        $this->success('请求成功', $list);
    }
    /*
     * 游戏详情
     */
    public function game_info()
    {
        if (!$id = input('id/d')) {
            $this->error(__('请选择游戏'));
        }
        $info = GameDirectory::where('id', '=', $id)->field('id,carousel_images,rich_text,video_url')->find();
        if($info){
            $imagesArray = explode(',', $info['carousel_images']);
            $resultArray = [];
            foreach ($imagesArray as $url) {
                $resultArray[] = ['image' => trim($url)]; // 使用 trim 去除多余的空格
            }
            $info['carousel_images'] =$resultArray;

            $videoUrls = array_filter(explode(',', $info['video_url']));
            $videoUrls = array_values($videoUrls);
            $info['video_url'] =$videoUrls;

        }
        $this->success('请求成功', $info);
    }

    /*
     * 新游体验
     */

    public function game_exp_list()
    {
        $where = []; // 初始化查询条件数组
        if(input('title')!=null&&input('title')!=''){
            $where['title']=['like','%'.input('title').'%'];
        }
        $where['shop_id']=input('shop_id');

        $query = GameExp::where('status' ,'=',1)->where('pla_status','=',1)->where('sh_status','<>',0);
        if (!empty($where)) {
            $query = $query->where($where);
        }
        $list = $query
            ->field('id,image_url,summary,title')
            ->order('weigh desc')
            ->paginate(10);
        foreach ($list as  $item) {
            $item->rating=round(GameRat::where('experience_id',$item->id)->avg('rating'),2);
        }
        $this->success('请求成功', $list);
    }



    /*
     * 评分
     */
    public function game_rat(){

        if (!$experience_id= input('experience_id')) {
            $this->error(__('请选择游戏'));
        }
        if (!$rating= input('rating')) {
            $this->error(__('请输入评分'));
        }
        if (!$shop_id= input('shop_id')) {
            $this->error(__('请选择店铺'));
        }
        if (!$feedback= input('feedback')) {
            $this->error(__('请填写感受'));
        }
        $result = GameRat::create([
            'user_id' => $this->auth->id,
            'feedback' =>$feedback,
            'shop_id'=>$shop_id,
            'rating' => $rating,
            'experience_id'=>$experience_id,
        ]);
        $this->success('提交成功', []);
    }

    //类型
    public function category(){
        $info = GameCategory::where('status', '=', 1)->where('pla_status', '=', 1)->where('sh_status','<>',0)->field('id,name')
            ->where('shop_id',input('shop_id'))
            ->select();
        $this->success('提交成功',$info);
    }
    //
    public function type(){
        $info = GameType::where('status', '=', 1)->where('pla_status', '=', 1)->where('sh_status','<>',0)->field('id,type')
            ->where('shop_id',input('shop_id'))
            ->select();
        $this->success('提交成功',$info);
    }


    //预约选人数
    public function num_list(){
        $data=[];
        $num=10;
        for ($i=1;$i<=$num;$i++){
            $data[]=$i;
        }
        $this->success('请求成功', $data);
    }

}
